import {
	objDeepCopy
} from "@/assets/js/common.js";
let Controller = {
	methods: {
		init() {
			var relId = this.Model.relId;
			this.getTags(this, relId);
		},
		//更新数组
		updata() {

		},
		backStyleInfo() {
			this.$router.push({
				path: 'attrList'
			})
		},
		//展开
		showMoreOption() {
			this.Model.showLength = this.Model.listData.length + 1;
		},
		//收起
		collapseOption() {
			this.Model.showLength = this.Model.showNo;
		},
		//点击确定 绑定标签
		updataTags() {
			var that = this;
			var bindArr = this.Model.bindArr;
			var apiTest = this.apiTest;
			var relId = this.Model.relId; //业务ID(外层的ID);
			var tempData = [];
			for (var i = 0; i < bindArr.length; i++) {
				var obj = {
					tagId: bindArr[i],
					relId: relId
				}
				tempData.push(obj);
			}
			this.$ajax({
				method: 'post',
				url: apiTest + '/sale/saleGoodsMaster/addBatchTagStatistics',
				// url:'/api/sale/saleGoodsMaster/addBatchTagStatistics',
				data: tempData
			}).then((res) => {
				console.log(res);
				var data = res.data;
				var obj = {};
				if (data.status == 200) {
					that.tips(that, 'success', '更新标签成功');
					that.getTags(that, that.Model.relId);

					setTimeout(function() {
						that.$router.push({
							path: 'attrList',
							query: {
								'form': 'child'
							}
						});
					}, 500)
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});

		},
		//获取tags
		getTags(that, id) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/sale/saleGoodsMaster/goodsTagList',
				// url:'/api/sale/saleGoodsMaster/goodsTagList',
				data: {
					goodsAttributesId: id
				}
			}).then((res) => {
				that.Model.listData = [];
				that.Model.bindArr = [];
				console.log(res);
				var data = res.data;
				var obj = {};
				if (data.status == 200) {
					var result = data.result;

					for (var i = 0; i < result.length; i++) {
						obj = objDeepCopy(result[i]);
						that.Model.listData.push(obj);
						// 
						var systemTags = result[i].systemTags;
						for (var j = 0; j < systemTags.length; j++) {
							if (systemTags[j].isSelected == true) {
								that.Model.bindArr.push(systemTags[j].id);
								console.log(that.Model.bindArr)
							}
						}
					}
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},
		handleSizeChange(val) {

		},
		handleCurrentChange(val) {

		},
		// 
		showChild(val) {
			console.log(val);
		},
		toSearch(val) { //查询
			console.log(val);
		},
		toClear(val) {
			console.log(val);
		},
		toExport(val) {
			console.log(val);
		},
		tips(that, type, message) {
			that.$message({
				message: message,
				type: type
			});
		}

	},
	watch: {
		//
		$route: {
			handler(newValue, oldValue) {
				if (newValue.query.relId) {
					this.getTags(this, newValue.query.relId);
				}
			},
			deep: true
		},

	}
}
export default Controller;
